package com.jqb.struts2lab02.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.jqb.struts2lab02.bean.User;

public class UserStrutsConverter extends StrutsTypeConverter {

	
	@Override
	@SuppressWarnings("rawtypes")
	public Object convertFromString(Map map, String[] values, Class c) {
		List<User> users = new ArrayList<User>();
		for (String userinfos : values) {
			String[] userinfo = userinfos.split(",");
			User user = new User();
			user.setUsername(userinfo[0]);
			user.setPassword(userinfo[1]);
			users.add(user);
		}
		return users;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public String convertToString(Map map, Object o) {
		List<User> users = (ArrayList<User>) o;
		StringBuffer sbf = new StringBuffer();
		for (User user : users) {
			sbf.append(user.getUsername() + ":" + user.getPassword() + "→");
		}
		return sbf.toString();
	}

}
